# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： user_page.py
# @Time：2023/8/4 18:03
# @Author：lizhixiong
from time import sleep

import allure
from selenium.webdriver.common.by import By

from common.commin.commin import object_left_click, object_send_keys, elements_visibility, get_ele_text
from common.log.log import log
from data.names import UserSel, PublicSel, FinancialSel
from po.jinhua.ht.jinhua_ht_main.jinhua_base import JHBase
from po.jinhua.ht.jinhua_ht_main.jinhua_ht_main_page import JHHTHomePage
from po.public_methods import assert_sel_text_exist


class UserPage(JHBase):
    """
    用户菜单下的所有页面
    """

    @allure.step('余额充值')
    def user_recharge(self, iphone, key, mode='add'):
        try:
            log.info('*****余额充值*****')
            data_info = '自动化测试'
            if mode == 'add':
                JHHTHomePage(self.driver).open_menu('yg', '余额充值')
                object_send_keys(self.driver, UserSel.el_user_001(), iphone)  # 搜索用户
                object_left_click(self.driver, PublicSel.el_icon_search_2())  # 点击搜索按钮
                object_left_click(self.driver, UserSel.el_user_002())  # 点击余额充值
                object_send_keys(self.driver, UserSel.el_user_005(), key)  # 输入充值金额
                object_send_keys(self.driver, UserSel.el_user_006(), data_info)  # 输入收付方公司
                object_send_keys(self.driver, UserSel.el_user_007(), data_info)  # 输入备注
                object_left_click(self.driver, UserSel.el_user_008())  # 点击确定
                sleep(0.5)
            elif mode == 'del':
                JHHTHomePage(self.driver).open_menu('yg', '余额充值')
                object_send_keys(self.driver, UserSel.el_user_001(), iphone)  # 搜索用户
                object_left_click(self.driver, PublicSel.el_icon_search_2())  # 点击搜索按钮
                object_left_click(self.driver, UserSel.el_user_002())  # 点击余额充值
                object_left_click(self.driver, UserSel.el_user_004())  # 点击减少
                object_send_keys(self.driver, UserSel.el_user_005(), key)  # 输入充值金额
                object_send_keys(self.driver, UserSel.el_user_006(), data_info)  # 输入收付方公司
                object_send_keys(self.driver, UserSel.el_user_007(), data_info)  # 输入备注
                object_left_click(self.driver, UserSel.el_user_008())  # 点击确定
        except Exception as e:
            log.error(f'余额充值，异常为{e}')
            raise e

    @allure.step('充值记录审核')
    def recharge_review(self, iphone, mode='pass'):
        try:
            log.info('*****充值记录审核*****')
            if mode == 'pass':
                JHHTHomePage(self.driver).open_menu('yg', '充值记录')
                object_send_keys(self.driver, FinancialSel.el_financial_006(), iphone)  # 搜索用户
                object_left_click(self.driver, FinancialSel.el_financial_007())  # 点击搜索按钮
                object_left_click(self.driver, FinancialSel.el_financial_001())  # 点击立即审核
                object_left_click(self.driver, FinancialSel.el_financial_004())  # 点击确定
            else:
                pass
        except Exception as e:
            log.error(f'充值记录审核，异常为{e}')
            raise e

    @allure.step('获取tips提示文案')
    def get_tips(self):
        try:

            elements_visibility(self.driver, By.XPATH, "//div[@role='alert']")
            tips = self.driver.find_element(By.XPATH, "//div[@role='alert']").text
            log.info(f'获取tips提示文案为：{tips}')
            sleep(2)  # 等待系统处理结束
            return tips
        except Exception as e:
            log.error(f'获取tips提示文案出现错误，异常为{e}')
            raise e

    @allure.step('搜索用户')
    def search_user(self, iphone):
        try:
            log.info('*****搜索用户*****')
            JHHTHomePage(self.driver).open_menu('yg', '用户管理')
            sleep(3)
            object_send_keys(self.driver, UserSel.el_user_010(), iphone)  # 搜索用户
            sleep(5)

            n = 0
            while n < 5:
                n += 1
                object_left_click(self.driver, UserSel.el_user_011())  # 点击搜索按钮
                sleep(1)
                if assert_sel_text_exist(self.driver, PublicSel.el_pagination_total(), '共 1 条'):
                    log.info(f'搜索用户成功，用户为：{iphone}')
                    break
                if n == 1:
                    raise '搜索出现异常，终止测试'
        except Exception as e:
            log.error(f'搜索用户，异常为{e}')
            raise e

    @allure.step('单独给某个用户发送优惠券')
    def send_coupon(self, iphone, coupon_name):
        try:
            self.search_user(iphone)
            object_left_click(self.driver, UserSel.el_user_013())  # 点击复选框
            object_left_click(self.driver, UserSel.el_user_012())  # 点击发送优惠券
            """优惠券弹窗"""
            object_send_keys(self.driver, UserSel.el_user_014(), coupon_name)  # 搜索优惠券
            object_left_click(self.driver, UserSel.el_user_015())  # 点击搜索
            object_left_click(self.driver, UserSel.el_user_016())  # 点击发送
            object_left_click(self.driver, PublicSel.el_ok_button())  # 二次弹窗点击确认

            sleep(0.5)
            return UserPage(self.driver)
        except Exception as e:
            log.error(f'充值记录审核，异常为{e}')
            raise e

    @allure.step('失效优惠券')
    def invalid_coupon(self, iphone, coupon_name):
        try:
            self.search_user(iphone)
            object_left_click(self.driver, UserSel.el_user_016_1())  # 点击更多
            object_left_click(self.driver, UserSel.el_user_016_2())  # 点击账户详情
            object_left_click(self.driver, UserSel.el_user_017())  # 点击持有优惠券
            sleep(2)
            object_left_click(self.driver, UserSel.el_user_018())  # 点击立即失效（定位到第1个)
            sleep(0.5)
            # 二次确定
            object_left_click(self.driver, PublicSel.el_ok_button())  # 点击确定
            return UserPage(self.driver)
        except Exception as e:
            log.error(f'充值记录审核，异常为{e}')
            raise e

    @allure.step('清除上级推广人')
    def clear_promoters(self, iphone):
        try:
            log.info('开始操作清除上级推广人')
            self.search_user(iphone)
            object_left_click(self.driver, UserSel.el_user_016_1())  # 点击更多
            object_left_click(self.driver, UserSel.el_user_016_3())  # 点击清除上级推广人
            object_left_click(self.driver, PublicSel.el_ok_button())  # 点击确定
        except Exception as e:
            log.error(f'清除上级推广人，异常为{e}')
            raise e

    @allure.step('获取列表的推荐人信息')
    def get_obtaining_referrals(self, iphone):
        try:
            log.info('获取列表的推荐人信息')
            self.search_user(iphone)
            sel_text = get_ele_text(self.driver, UserSel.el_user_016_5(), mode=1)
            log.info(f'获取列表的推荐人信息为：{sel_text}')
            return sel_text
        except Exception as e:
            log.error(f'获取列表的推荐人信息，异常为{e}')
            raise e

    @allure.step('修改上级推广人')
    def modify_promoters(self, iphone_1, iphone_2):
        try:
            log.info('开始修改上级推广人')
            self.search_user(iphone_1)
            object_left_click(self.driver, UserSel.el_user_016_1())  # 点击更多
            object_left_click(self.driver, UserSel.el_user_016_4())  # 点击修改上级推广人
            object_left_click(self.driver, UserSel.el_xg_01())  # 点击选择用户图标
            object_send_keys(self.driver, UserSel.el_xg_03(), iphone_2)  # 搜索用户2
            object_left_click(self.driver, UserSel.el_xg_04())  # 点击搜索
            object_left_click(self.driver, UserSel.el_xg_05())  # 列表复选框，勾选
            object_left_click(self.driver, UserSel.el_xg_06())  # 点击确定
            object_left_click(self.driver, UserSel.el_xg_02())  # 点击确定
        except Exception as e:
            log.error(f'修改上级推广人，异常为{e}')
            raise e
